package com.luomu.base.biz.cookBook.excel.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.luomu.base.api.constant.BaseRspConstants;
import com.luomu.base.api.cookBook.excel.bo.AddressInfoExportReqBO;
import com.luomu.base.api.cookBook.excel.bo.AddressInfoExportRspBO;
import com.luomu.base.api.cookBook.excel.bo.AddressInfoImportBO;
import com.luomu.base.api.cookBook.excel.bo.AddressInfoImportRspBO;
import com.luomu.base.api.cookBook.excel.service.ExpressAddressExcelService;
import com.luomu.base.api.exception.ZTBusinessException;
import com.luomu.base.biz.cookBook.address.impl.ExpressAddressServiceImpl;
import com.luomu.base.biz.cookBook.excel.listener.AddressInfoListener;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("/expressAddressService")
public class ExpressAddressExcelServiceImpl implements ExpressAddressExcelService {
    private Logger logger = LoggerFactory.getLogger(ExpressAddressServiceImpl.class);

    @Override
    public AddressInfoImportRspBO importAddressExcel(MultipartFile file, Long openId, String AgentId) {
        logger.info("-------------【地址导入开始】-----------{},{}",JSON.toJSONString(openId),JSON.toJSONString(AgentId));
        AddressInfoImportRspBO rspBaseBO = new AddressInfoImportRspBO();
        try {
            //获取文件的输入流
            InputStream inputStream = file.getInputStream();
            AddressInfoListener addressInfoListener = new AddressInfoListener();
            EasyExcel.read(inputStream) //调用read方法
                    //注册自定义监听器，字段校验可以在监听器内实现
                    .registerReadListener(addressInfoListener)
                    .excelType(ExcelTypeEnum.XLSX)
                    .head(AddressInfoImportBO.class) //对应导入的实体类
                    .sheet(0) //导入数据的sheet页编号，0代表第一个sheet页，如果不填，则会导入所有sheet页的数据
                    .headRowNumber(1) //列表头行数，1代表列表头有1行，第二行开始为数据行
                    .doRead(); //开始读Excel，返回一个List<T>集合，继续后续入库操作

            if (CollectionUtils.isNotEmpty(addressInfoListener.getTips())){
                rspBaseBO.setTips(addressInfoListener.getTips());
                rspBaseBO.setMessage(BaseRspConstants.RSP_DESC_SUCCESS);
                rspBaseBO.setCode(BaseRspConstants.RSP_CODE_SUCCESS);
            }else {
                List<AddressInfoImportBO> addressInfoImportBOS = addressInfoListener.getDatas();
                addressInfoImportBOS.forEach(item -> System.out.println(JSON.toJSONString(item)));
            }

        } catch (Exception exception) {
            exception.printStackTrace();
            logger.error("importAddressExcel====>地址信息导入失败："+ exception.getMessage());
            throw new ZTBusinessException("地址信息导入失败");
        }
        return rspBaseBO;

    }


}
